﻿CREATE PROCEDURE [dbo].[proc_Customer_GetEmployeeList_Service]
(
	@UserName nvarchar(50),
	@StartIndex int,
	@EndIndex int
)
As
Begin
	declare @companyid int,@Days int,@DepartmentId int
	EXEC @companyid=proc_Company_GetID @UserName,@companyid--单位ID
	set @DepartmentId = (Select DepartmentId From Employee Where UserName=@UserName)--部门Id
	Set @Days = (Select ProtectDays From Department Where id=@DepartmentId)--系统保护天数
	;WITH list As (Select ROW_NUMBER() OVER (ORDER BY TrackDate DESC) AS Row,
		Id,
		delFlag,
		CustomerId,
		IsNull(CustomerName,'...') as CustomerName,
		Project,
		ProtectId,
		SourceId,
		IndustryOneId,
		TypeId,
		CreateDate,		
		IsNull(ProtectDate,'1900-01-01') As ProtectDate,
		IsNull((@Days-datediff(day,Isnull(ProtectDate,GetDate()),GetDate())),0) As Countdown,
		ProtectName
	From Customer
	Where Companyid=@companyid And ServicePerson=@UserName And delflag=0 And Audit=1)

	Select *,
	IsNull((Select Title From Customer_Source Where Id=list.SourceId),'...') As Source,
	IsNull((Select Title From Customer_Industry Where Id=list.IndustryOneId),'...') As IndustryOne,
	IsNull((Select Title From Customer_Type Where Id=list.TypeId),'...') As Type,
	IsNull((Select Title From Customer_Protect Where Id=list.ProtectId),'...') As Protect,
	IsNull((select Ename from employee where employee.username=ProtectName),'') ProtectNameEName,
	(Select Count(0) From list) As RecordCount
	From list
	Where Row Between @StartIndex and @EndIndex Order By Row
End
